//
//  MineYouHuiJuanController.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/12.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class MineYouHuiJuanController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    let CellId:String = "CellId"
    
    var tableView:UITableView = UITableView.init()
    
    let bottomView:UIView = UIView.init()
    
    let titleNames:[String] = ["未使用","已使用","已过期"]
    
    let bgView:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: k_Wide, height: 64))

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        self.createTableview()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavi()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        bgView.alpha = 0.0
    }
}
extension MineYouHuiJuanController {
    //MARK:设置导航栏选项
    func setNavi(){
        self.navigationItem.title = "优惠券"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", imagStr: "ic_back", fontSize: 14, target: self, action: #selector(leftBarButtonAction))
        bgView.backgroundColor = .white
        self.navigationController?.navigationBar.subviews.first?.addSubview(bgView)
    }
    //MARK:leftBarButtonAction
    func leftBarButtonAction(){
        self.navigationController!.popViewController(animated: true)
    }
    func setUI(){
        let orignY:CGFloat = 64
        for i in 0 ..< self.titleNames.count {
            let titleLabel:UILabel = UILabel.init()
            let conTrol:UIControl = UIControl.init()
            self.view.addSubview(titleLabel)
//            self.view.addSubview(bottomView)
            titleLabel.snp.makeConstraints({ (make) in
                make.left.equalTo(CGFloat(i) * (k_Wide/3))
                make.width.equalTo(k_Wide/3)
                make.top.equalTo(10 + orignY)
                make.height.equalTo(15)
            })
            titleLabel.font = UIFont.systemFont(ofSize: 15)
//            bottomView.snp.makeConstraints({ (make) in
//                make.left.equalTo(titleLabel)
//                make.bottom.equalTo(titleLabel)
//                make.height.equalTo(1)
//                make.width.equalTo(k_Wide/3)
//            })
            titleLabel.text = titleNames[i]
            titleLabel.textAlignment = .center
            self.view.addSubview(conTrol)
            conTrol.snp.makeConstraints({ (make) in
                make.left.equalTo(titleLabel)
                make.height.equalTo(titleLabel)
                make.top.equalTo(titleLabel)
                make.width.equalTo(titleLabel)
            })
            conTrol.tag = 1000 + i
            conTrol.addTarget(self, action: #selector(conTrolAction(_:)), for: .touchUpInside)
            self.view.addSubview(self.bottomView)
            self.bottomView.snp.makeConstraints({ (make) in
                make.left.equalTo(0)
                make.size.equalTo(CGSize.init(width: k_Wide/3, height: 1))
                make.top.equalTo(34 + 64)
            })
            self.bottomView.backgroundColor = .red
        }
    }
    func conTrolAction(_ conTrol:UIControl){
        switch conTrol.tag {
        case 1000:
            self.bottomView.snp.removeConstraints()
            self.bottomView.snp.makeConstraints({ (make) in
                make.left.equalTo(0)
                make.size.equalTo(CGSize.init(width: k_Wide/3, height: 1))
                make.top.equalTo(34 + 64)
            })
            break
        case 1001:
            self.bottomView.snp.removeConstraints()
            self.bottomView.snp.makeConstraints({ (make) in
                make.left.equalTo(k_Wide/3)
                make.size.equalTo(CGSize.init(width: k_Wide/3, height: 1))
                make.top.equalTo(34 + 64)
            })
            break
        case 1002:
            self.bottomView.snp.removeConstraints()
            self.bottomView.snp.makeConstraints({ (make) in
                make.left.equalTo((k_Wide/3) * 2)
                make.size.equalTo(CGSize.init(width: k_Wide/3, height: 1))
                make.top.equalTo(34 + 64)
            })
            break
        default:
            break
        }
    }
    //MARK:tableviewCreate
    func createTableview(){
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.top.equalTo(35+64)
            make.bottom.equalTo(0)
            make.right.equalTo(0)
        }
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UITableViewCell.self , forCellReuseIdentifier: CellId)
    }
    //MARK:TableViewDelegate
    @objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击")
    }
    @objc(numberOfSectionsInTableView:) func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell.init()
    }
}
